package co.yixiang.modules.system.service.impl;

import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.modules.system.domain.GuidancePrice;
import co.yixiang.modules.system.dto.GuidancePriceDto;
import co.yixiang.modules.system.dto.queryCriteria.GuidancePriceQueryCriteria;
import co.yixiang.modules.system.mapper.GuidancePriceMapper;
import co.yixiang.modules.system.service.GuidancePriceService;
import co.yixiang.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @author zf
 * @date 2021-07-10
 */
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "guidancePrice")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class GuidancePriceServiceImpl extends BaseServiceImpl<GuidancePriceMapper, GuidancePrice> implements GuidancePriceService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(GuidancePriceQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<GuidancePrice> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(GuidancePriceDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<GuidancePrice> queryAll(GuidancePriceQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(GuidancePrice.class, criteria));
    }


    @Override
    public void download(List<GuidancePriceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GuidancePriceDto guidancePrice : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("行政区", guidancePrice.getDistrict());
            map.put("街道", guidancePrice.getStreet());
            map.put("项目", guidancePrice.getProject());
            map.put("项目成交价", guidancePrice.getMoney());
            map.put("创建时间", guidancePrice.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


}
